package com.gestocasa.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RegisterValue {

	private Attribute attribute;
	private Value value;
	
	public RegisterValue() {
		this(null, null);
	}
	
	public RegisterValue(RegisterValue registerValue) {
		this(registerValue.getAttribute(), registerValue.getValue()); 
	}
	
	public RegisterValue(Attribute attribute, Value value) {
		this.attribute = attribute;
		this.value = value;
	}
	
	public Attribute getAttribute() {
		return attribute;
	}

	public void setAttribute(Attribute attribute) {
		this.attribute = attribute;
	}

	public Value getValue() {
		return value;
	}

	public void setValue(Value value) {
		this.value = value;
	}
	
	public void setCustomValue(Object value) {
		if(value != null) {
//			if(attribute != null) {
//				if(Attribute.TYPE_DATE.equals(attribute.getType())) {
//					try {
//						String stringDate = (String)value + " 00:00:00";
//						this.value = new DateValue(SimpleDateFormat.getInstance().parse(stringDate));
//					} catch (ParseException e) {
//						e.printStackTrace();
//					}
//				}
//				else if(Attribute.TYPE_NUMBER.equals(attribute.getType())) {
//					this.value = new NumericValue(Double.parseDouble((String)value));
//				}
//				else if(Attribute.TYPE_STRING.equals(attribute.getType())) {
//					this.value = new StringValue(value.toString());
//				}
//			}
//			else {
//				if(value instanceof Date) {
//					this.value = new DateValue((Date)value);
//				}
//				else if(value instanceof Double) {
//					this.value = new NumericValue((Double)value);
//				}
//				else {
//					this.value = new StringValue(value.toString());
//				}	
//			}
			
			if(Attribute.TYPE_DATE.equals(attribute.getType())) {
				if(value instanceof String) {
					try {
						String stringDate = (String)value + " 00:00:00";
						this.value = new DateValue(SimpleDateFormat.getInstance().parse(stringDate));
					} catch (ParseException e) {
						this.value = new DateValue(new Date());
					}
				}
				else {
					this.value = new DateValue((Date)value);
				}
			}
			else if(Attribute.TYPE_NUMBER.equals(attribute.getType())) {
				if(value instanceof String) {
					this.value = new NumericValue(Double.parseDouble((String)value));
				}
				else {
					this.value = new NumericValue((Double)value);
				}
			}
			else if(Attribute.TYPE_STRING.equals(attribute.getType())) {
				this.value = new StringValue(value.toString());
			}
		}
	}
	
	@Override
	public String toString() {
		return value.toString();
	}
}
